#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   http_name.sh
# Version:    1.0
# Date:       2013/11/08
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-08Apache-01字符模式-03搭建虚拟站点-01基于名称的虚拟服务器
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/08
#             - apache supports virtual services based on name.
# ----------------------------------------------------------------------

##! @TODO: setup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_setup()
{
    serv=httpd

    server_ip=`ifconfig -a | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
    if [ -z ${server_ip} ]; then
        echo "failed: get local ip null, pls check"
        exit 1
    fi

    # create dirs
    server_dir1=/var/www/test1
    server_dir2=/var/www/test2
    mkdir ${server_dir1} ${server_dir2}
    EchoResult "mkdir test1 test2"

    # chmod dirs
    chmod -R 777 ${server_dir1} ${server_dir2}
    EchoResult "chmod test1 test2"

    # create index.html 
    echo test1 > ${server_dir1}/index.html
    echo test2 > ${server_dir2}/index.html

    # http service
    # back httpd.conf
    HTTPCONF=/etc/httpd/conf/httpd.conf
    cp ${HTTPCONF} ${HTTPCONF}.bak
    EchoResult "back ${HTTPCONF}"

    # add NameVirtualHost to httpd.conf
    server_name1=www.test1.com
    server_name2=www.test2.com
    cat >> ${HTTPCONF}  <<-EOF
NameVirtualHost *:80
<VirtualHost *:80>
        DocumentRoot ${server_dir1}
        Servername ${server_name1}
</VirtualHost>
<VirtualHost *:80>
        DocumentRoot ${server_dir2}
        Servername ${server_name2}
</VirtualHost>
EOF

    # restart
    restart_daemon ${serv}

    sleep 3
    # check httpd restart
    status_daemon ${serv}  && netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} restart"


    #
    # add /etc/hosts
    HOSTS=/etc/hosts
    cp ${HOSTS} ${HOSTS}.bak

    cat >> ${HOSTS} <<-EOF
${server_ip} ${server_name1}
${server_ip} ${server_name2}
EOF

    trap do_cleanup INT EXIT

}

##! @TODO: test
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    rm -f /tmp/index.html*
    cd /tmp
    wget http://${server_name1}
    wget http://${server_name2}

    cd -
    

    [ -f /tmp/index.html ] && diff /tmp/index.html ${server_dir1}/index.html
    EchoResult "wget http://${server_name1}"

    [ -f /tmp/index.html.1 ] && diff /tmp/index.html.1 ${server_dir2}/index.html
    EchoResult "wget http://${server_name2}"
    
}

##! @TODO: cleanup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_cleanup()
{
    rm -rf ${server_dir1} ${server_dir2}
    mv -f ${HTTPCONF}.bak ${HTTPCONF}
    stop_daemon ${serv}

    rm -f /tmp/index.html*
    mv -f ${HOSTS}.bak ${HOSTS}
}

# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh


do_setup
do_test
EchoResult "testcase : ${serv} support virtual services based on name"
